#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

int gcnt = 100;

void *ThreadRoutine(void *args)
{
    // usleep(1000);   // 让主线程先打印
    const char *threadname = (const char *)args;
    while(true)
    {
        std::cout << "I am a new thread：" << threadname << ", pid: " << getpid() 
                  << " gcnt: " << gcnt << " &gcnt: " << &gcnt << std::endl;
        gcnt--;
        sleep(1); 
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, ThreadRoutine, (void*)"thread 1");   // 创建线程

    // 主线程
    while(true)
    {
        std::cout << "I am main thread" << ", pid: " << getpid() 
                << " gcnt: " << gcnt << " &gcnt: " << &gcnt << std::endl;
        sleep(1);
    }
    return 0;
}